package com.wbdgj.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.HttpSerives;
import com.wbdgj.utils.LogUtils;
import com.wbdgj.utils.ToastUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import butterknife.ButterKnife;

/**
 * Created by cc on 16-7-9.
 */
public abstract class BaseWhiteFragment extends Fragment {
    protected View mainView;
    protected boolean isViewInitiated;
    protected boolean misVisibleToUser;
    protected boolean isDataInitiated;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public HttpSerives getServer() {
        return HttpAdapter.getSerives();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //SkinManager.getInstance().register(getActivity());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getActivity().getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }


        if (getLayout() <= 0) {
            mainView = createMainView();
        } else {
            mainView = inflater.inflate(getLayout(), container, false);
        }
        if (mainView == null)
            throw new NullPointerException("hey,you forget set a view for " + getClass().getName());
        ButterKnife.bind(this, mainView);
        return mainView;
    }


    /**
     * 是否适应沉浸式
     *
     * @return
     */
    public boolean getFitSystem() {
        return false;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onInit();
        if (onInitCompleteListener != null)
            onInitCompleteListener.onInitComplete();
//        if (getFitSystem()) {
//            setFitSystem();
//        }
    }

    protected View findViewById(@IdRes int id) {
        return mainView.findViewById(id);
    }

    @LayoutRes
    protected abstract int getLayout();

    protected View createMainView() {
        return null;
    }

    /**
     * 数据初始化
     */
    protected abstract void onInit();

    protected void launch(Class clazz) {
        startActivity(new Intent(getActivity(), clazz));
    }

    protected void launch(Class clazz, Intent intent) {
        intent.setClass(getActivity(), clazz);
        startActivity(intent);
    }

    protected void showToast(String msg) {
        ToastUtils.toastShort(msg);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //EventBus.getDefault().unregister(this);
    }

    OnInitCompleteListener onInitCompleteListener;

    public void setOnInitCompleteListener(OnInitCompleteListener onInitCompleteListener) {
        this.onInitCompleteListener = onInitCompleteListener;
    }

    public interface OnInitCompleteListener {
        void onInitComplete();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareFetchData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtils.logi("------setUserVisibleHint当前加载的类名=" + getClass().getName());
//        LogUtils.logi("-----开始界面isVisibleToUser=" + isVisibleToUser);
//        LogUtils.logi("-----开始界面getUserVisibleHint()=" + getUserVisibleHint());
        if (getUserVisibleHint()) {
            this.misVisibleToUser = true;
            // LogUtils.logi("-----开始界面="+isVisibleToUser);
        } else {
            if (misVisibleToUser) {
                this.misVisibleToUser = false;
            }
        }
        //LogUtils.logi("-----isVisibleToUser的值="+isVisibleToUser);
        prepareFetchData(isVisibleToUser);
    }

    public abstract void fetchData();

    public boolean prepareFetchData() {
        return prepareFetchData(false);
    }

    public boolean prepareFetchData(boolean forceUpdate) {
        if (misVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            fetchData();
            // LogUtils.logi("----- getClass().getName()=" + forceUpdate);
            isDataInitiated = true;
            return true;
        }
        return false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // SkinManager.getInstance().unregister(getActivity());
    }


}
